<template>
  <div class="login-wrap">
    <div class="login_box">
      <div class="login_l_img"><img src="../assets/img/login-img.png"/></div>
      <div class="login">
        <div class="login_logo"><a href="#"><img src="../assets/img/login_logo.png"/></a></div>
        <div class="login_name">
          <p>西南石油大学综合管理系统</p>
        </div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
          <el-form-item prop="project">

            <el-select v-model="ruleForm.project" placeholder="请选择" style="width:100%">
              <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
              <el-option key="test" label="测试环境" value="test"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="username">
            <el-input v-model="username" placeholder="用户名">
              <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                type="password"
                placeholder="密码"
                v-model="ruleForm.password"
                @keyup.enter.native="submitForm('ruleForm')"
            >
              <el-button slot="prepend" icon="el-icon-eleme"></el-button>
            </el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </div>
        </el-form>
      </div>
      <div class="copyright">西南石油大学综合管理系统版权所有</div>
    </div>
  </div>
</template>

<script>
import {login_Url} from "@/config/api";
import CryptoJS from "crypto-js";
import axios from 'axios'
//加密的私钥
let key = '12345678';//自定义

export default {
  data: function () {
    return {
      test_ev: "",
      fullName: "",
      ruleForm: {
        project: "test",
        username: "",
        password: "",
      },
      rules: {

        project: [
          {
            required: true,
            message: "请选择环境",
            trigger: "blur"
          }
        ],
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"}
        ],
        password: [{required: true, message: "请输入密码", trigger: "blur"}]
      }
    };
  },

  computed: {
    username: {
      get: function () {
        return this.ruleForm.username;
      },
      set: function (val) {
        this.ruleForm.username = val.toUpperCase();
      }
    }
  },

  created() {
    this.setUrl();
  },

  methods: {

    // 全局设置请求地址
    setUrl() {
      // 1. 测试环境
      this.test_ev = 'http://10.168.11.59:7203/api';

      // 2. 本机环境 取消注释就是本机环境
      this.local_ev = 'http://localhost:20000';
      this.test_ev = this.local_ev;
      this.changeUrl();
    },

    // 全局修改请求地址
    changeUrl() {
      if (this.ruleForm.project === 'test') {
        // 测试环境切换
        sessionStorage.setItem("base_url", this.test_ev);
        this.axios.defaults.baseURL = sessionStorage.getItem("base_url");
      }
    },

    submitForm(formName) {
      this.changeUrl();
      this.$refs[formName].validate(valid => {
        if (valid) {
          login_Url({
            username: this.ruleForm.username,
            password: this.ruleForm.password,
            project: this.ruleForm.project
          }).then(res => {
            if (this.ruleForm.project === "test") {
              this.$store.commit("set_token", res.data.data);
              localStorage.clear();
              localStorage.setItem("set_token", res.data.data);
              if (res.data.status === false) {
                this.$message.error("登录失败");
              } else {
                localStorage.setItem("ms_username", this.ruleForm.username);
                this.$router.push("/Dashboard");
              }
            } else {
              this.$store.commit("set_token", res.data.data.token);
              localStorage.clear();
              localStorage.setItem("set_token", res.data.data.token);
              localStorage.setItem("project_id", this.ruleForm.project);
              if (res.data.success === false) {
                this.$message.error("登录失败");
              } else {
                localStorage.setItem("ms_username", this.ruleForm.username);
                this.$router.push("/Dashboard");
              }
            }
            localStorage.setItem("user_type", "1");
          });

        } else {
          console.log("error submit!!");
          return false;
        }
      });

    },

    // DES加密
    encryptByDES(message) {//传入加密的内容
      //把私钥转换成16进制的字符串
      var keyHex = CryptoJS.enc.Utf8.parse(key);
      //模式为ECB padding为Pkcs7
      var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
      //加密出来是一个16进制的字符串
      return encrypted.ciphertext.toString();
    }
  },
};
</script>
<style scoped>
.login-wrap {
  height: 100%;
  overflow: hidden;
  background: #93defe;
  background-size: cover;
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #000000;
  border-bottom: 1px solid #ddd;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}


.login_box {
  width: 1100px;
  margin: 120px auto 0;
}

.login_box .login_l_img {
  float: left;
  width: 432px;
  height: 440px;
  margin-left: 50px;
}

.login_box .login_l_img img {
  width: 500px;
  height: 440px;
}

.login {
  height: 360px;
  width: 400px;
  padding: 50px;
  background-color: #ffffff;
  border-radius: 6px;
  box-sizing: border-box;
  float: right;
  margin-right: 50px;
  position: relative;
  margin-top: 50px;
}

.login_logo {
  width: 120px;
  height: 120px;
  border: 5px solid #93defe;
  border-radius: 100px;
  background: #fff;
  text-align: center;
  line-height: 110px;
  position: absolute;
  top: -60px;
  right: 140px;
}

.login_name {
  width: 100%;
  float: left;
  text-align: center;
  margin-top: 20px;
}

.login_name p {
  width: 100%;
  text-align: center;
  font-size: 18px;
  color: #444;
  padding: 10px 0 20px;
}

.login_logo img {
  width: 60px;
  height: 60px;
  display: inline-block;
  vertical-align: middle;
}

input[type=text], input[type=file], input[type=password], input[type=email], select {
  border: 1px solid #DCDEE0;
  vertical-align: middle;
  border-radius: 3px;
  height: 50px;
  padding: 0px 16px;
  font-size: 14px;
  color: #555555;
  outline: none;
  width: 100%;
  margin-bottom: 15px;
  line-height: 50px;
  color: #888;
}

input[type=text]:focus, input[type=file]:focus, input[type=password]:focus, input[type=email]:focus, select:focus {
  border: 1px solid #27A9E3;
}

input[type=submit], input[type=button] {
  display: inline-block;
  vertical-align: middle;
  padding: 12px 24px;
  margin: 0px;
  font-size: 16px;
  line-height: 24px;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  color: #ffffff;
  background-color: #27A9E3;
  border-radius: 3px;
  border: none;
  -webkit-appearance: none;
  outline: none;
  width: 100%;
}

.copyright {
  font-size: 14px;
  color: #fff;
  display: block;
  width: 100%;
  float: left;
  text-align: center;
  margin-top: 60px;
}
</style>
